package com.glut.campusrepair.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.glut.campusrepair.config.QueryPageParam;
import com.glut.campusrepair.config.Result;
import com.glut.campusrepair.dto.RepairPersonnelDto;
import com.glut.campusrepair.service.IRepairPersonnelService;
import com.glut.campusrepair.entity.RepairPersonnel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/repair")
@CrossOrigin(origins = "http://localhost:5173") // 添加跨域注解
public class RepairPersonnelController {

    @Autowired
    private IRepairPersonnelService repairPersonnelService;

    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody Map<String, String> loginRequest) {
        String username = loginRequest.get("username");
        String password = loginRequest.get("password");

        RepairPersonnel repairPersonnel = repairPersonnelService.getOne(new QueryWrapper<RepairPersonnel>().eq("username", username).eq("password", password));
        Map<String, Object> response = new HashMap<>();
        if (repairPersonnel != null) {
            response.put("success", true);
            response.put("message", "登录成功");
            response.put("repairPersonnel", repairPersonnel);
        } else {
            response.put("success", false);
            response.put("message", "用户名或密码错误");
        }
        return response;
    }

    @GetMapping("/me")
    public Map<String, Object> getUserInfo(@RequestParam Integer repairPersonnelId) {
        RepairPersonnel repairPersonnel = repairPersonnelService.getById(repairPersonnelId);
        Map<String, Object> response = new HashMap<>();
        if (repairPersonnel != null) {
            response.put("success", true);
            response.put("repairPersonnel", repairPersonnel);
        } else {
            response.put("success", false);
            response.put("message", "用户信息获取失败");
        }
        return response;
    }

    //linghu

//    @Autowired
//    private IRepairPersonnelService repairPersonnelService; // 注入维修人员服务

    /**
     * 获取所有维修人员信息
     * @return 所有维修人员信息
     */
    @GetMapping("/list")
    public List<RepairPersonnel> getAllRepairPersonnel() {
        return repairPersonnelService.list();
    }

    /**
     * 新增维修人员
     * @param repairPersonnel 维修人员信息
     */
    @PostMapping("/save")
    public boolean addRepairPersonnel(@RequestBody RepairPersonnel repairPersonnel) {
        return repairPersonnelService.save(repairPersonnel);
    }

    /**
     * 修改维修人员
     * @param repairPersonnel 维修人员信息
     */
    @PostMapping("/mod")
    public boolean modRepairPersonnel(@RequestBody RepairPersonnel repairPersonnel) {
        return repairPersonnelService.updateById(repairPersonnel);
    }

    /**
     * 新增或修改维修人员
     * @param repairPersonnel 维修人员信息
     */
    @PostMapping("/saveOrMod")
    public boolean saveOrMod(@RequestBody RepairPersonnel repairPersonnel) {
        return repairPersonnelService.saveOrUpdate(repairPersonnel);
    }

    /**
     * 删除维修人员
     * @param id 维修人员ID
     */
    @GetMapping("/delete")
    public boolean deleteRepairPersonnel(@RequestParam("repair_personnel_id") Integer id) {
        return repairPersonnelService.removeById(id);
    }

    /**
     * 模糊查询维修人员
     * @param repairPersonnel 维修人员信息
     */
    @PostMapping("/ListP")
    public List<RepairPersonnel> listRepairPersonnel(@RequestBody RepairPersonnel repairPersonnel) {
        LambdaQueryWrapper<RepairPersonnel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(RepairPersonnel::getFullName, repairPersonnel.getFullName());
        // 可以根据需要增加更多的查询条件，例如性别、维护类型等
        return repairPersonnelService.list(lambdaQueryWrapper);
    }



    /**
     * 分页查询维修人员，下面这个方法是Ipage的分页查询方法,自定义，相关方法需要自己创建
     */

    @PostMapping("/listRepairPersonnelPage1")
    public Result listRepairPersonnelPage1(@RequestBody QueryPageParam query) {
        HashMap<String, Object> param = query.getParam();
        String username = (String) param.get("username");
        String gender = (String) param.get("gender");

        Page<RepairPersonnel> page = new Page<>(query.getPageNum(), query.getPageSize());

        LambdaQueryWrapper<RepairPersonnel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (username != null && !username.isEmpty()) {
            lambdaQueryWrapper.like(RepairPersonnel::getUsername, username);
        }
        if (gender != null && !gender.isEmpty()) {
            lambdaQueryWrapper.eq(RepairPersonnel::getGender, gender);
        }

        IPage<RepairPersonnel> result = repairPersonnelService.page2(page, lambdaQueryWrapper);
        System.out.println("total==" + result.getTotal());

        return Result.success(result.getRecords(), result.getTotal());
    }

    @GetMapping("/setId/{username}")
    public Integer getRepairPersonnelId(@PathVariable String username){
        return repairPersonnelService.getRepairPersonnelId(username);
    }
    /**
     * 批量删除维修人员
     *
     */
    @PostMapping("/deleteBatch")
    public boolean deleteBatch(@RequestBody RepairPersonnelDto repairPersonnelDto) {
        List<Integer> ids = repairPersonnelDto.getRepairPersonnelIdList();
        return repairPersonnelService.removeByIds(ids);
    }

}
